package com.net.shop.car.manager.ui.personalcenter.dingdan;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Dingdan;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.dingdan.GetDingdanOrderList;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanOrderListResponse;

public class NormalListAdapter extends BaseAdapter implements OnItemClickListener {

	private List<Dingdan> dingdans = new ArrayList<Dingdan>();
	
	private int totalPages;
	
	private int pageIndex;
	
	private int postType;
	
	private Member user;
	
	private OnDingDanClickedListener onDingDanClickedListener;
	
	public interface OnDingDanClickedListener {
		void onDingdanClicked(Dingdan dingdan);
	}
	
	/**
	 * 
	 */
	private BaseActivity activity;
	
	public NormalListAdapter(BaseActivity activity,int postType,Member user,
			OnDingDanClickedListener onDingDanClickedListener) {
		this.activity = activity;
		this.postType = postType;
		this.user = user;
		pageIndex = 1;
		this.onDingDanClickedListener = onDingDanClickedListener;
		getData();
	}
	
	/**
	 * 
	 */
	private void getData() {
		activity.progressDialog.show();
		VolleyCenter.getVolley().addGetRequest(new GetDingdanOrderList(user.getMemberId(), 
				postType, pageIndex++), 
				new VolleyListenerImpl<DingdanOrderListResponse>(new DingdanOrderListResponse()) {

					@Override
					public void deleveryResponse(
							DingdanOrderListResponse response) {
						activity.progressDialog.dismiss();
						if (response.isSuccess()) {
							totalPages = response.getTotalPages();
							dingdans.addAll(response.getDingdans());
							notifyDataSetChanged();
						}
					}
		});
	}

	@Override
	public int getCount() {
		if (dingdans.size() == 0) {
			return 1;
		} else {
			if (pageIndex <= totalPages) {
				return dingdans.size() + 1;
			}
		}
		return dingdans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (dingdans.size() == 0) {
			return getTextView("暂无数据", parent);//无数据的情况
		}
		if (pageIndex <= totalPages && position == getCount() -1) {
			TextView textView = getTextView("加载更多", parent);//当滑动到底部还有更多数据时
			textView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					getData();
				}
			});
			return textView;
		}
		
		View currentView = convertView;
		if (currentView == null || currentView instanceof TextView) {
			currentView = activity.inflater.inflate(R.layout.dingdanorder_listitem, parent, false);
			currentView.setTag(new DingDanOrderItem(currentView));
		}
		DingDanOrderItem danOrderItem = (DingDanOrderItem) currentView.getTag();
		danOrderItem.setData(dingdans.get(position));
		return currentView;
	}
	
	/**
	 * @param txt
	 * @return
	 */
	private TextView getTextView(String txt,ViewGroup viewGroup) {
		TextView textView = (TextView) activity.inflater.inflate(R.layout.listitem_txt, viewGroup, false);
		textView.setText(txt);
		textView.setClickable(true);
		return textView;
	}
	
	private class DingDanOrderItem {
		private TextView name;
		private TextView des;
		private TextView right;
		
		public DingDanOrderItem(View view) {
			name = (TextView) view.findViewById(R.id.dingdanorder_listitem_name);
			des = (TextView) view.findViewById(R.id.dingdanorder_listitem_des);
			right = (TextView) view.findViewById(R.id.dingdanorder_listitem_right);
		}
		
		public void setData(Dingdan dingdan) {
			name.setText(dingdan.getGoodName());
			if (postType == 6) {
				right.setText("当面付");
			} else if (postType == 5) {
				if (dingdan.getUseVb() > 0) {
					des.setText("已使用积分抵扣");
				} else {
					des.setText("");
				}
				right.setText(dingdan.getGoodPrice().toString());
			} else if (postType == 4) {
				right.setText("积分兑换");
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (onDingDanClickedListener != null) {
			onDingDanClickedListener.onDingdanClicked(dingdans.get(position));
		}
	}

}
